package org.jeecg.modules.business.third.strategy.sap;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.basic.plant.service.IWmsSapWarehouseMapService;
import org.jeecg.modules.business.third.entity.WmsInReceiptMaterialVo;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTaskMaterial;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskMaterialService;
import org.jeecg.modules.dock.entity.dto.request.SaleOutboundRequestDTO;
import org.jeecg.modules.dock.entity.dto.request.SellOutboundRequestDTO;
import org.jeecg.modules.dock.entity.dto.response.SellOutboundResponseDTO;
import org.jeecg.modules.dock.service.SapDockingRepeaterService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/*
 * 项目名称:    wms-server-jeecg
 * 接口描述:    销售退货(定尺/铁心/铁心柱) 入库：WMS——>SAP【接口：A809】" 'R28
 * 创建人员:    zhaojun
 * 创建时间:    2023-10-23
 */
@Service(value = "r28SapWarehouse")
@Slf4j
@RequiredArgsConstructor
public class R28SapWarehouse implements SapOperateStrategy {

    private final IWmsInReceiptTaskMaterialService wmsInReceiptTaskMaterialService;
    private final IWmsSapWarehouseMapService wmsSapWarehouseMapService;
    private final SapDockingRepeaterService sapDockingRepeaterService;

    @Override
    public List operate(Map<String, Object> map) {
        log.info("/销售退货(定尺/铁心/铁心柱) 入库");
        List<WmsInReceiptMaterialVo> wmsInReceiptMaterialVos = (List<WmsInReceiptMaterialVo>) map.get("records");

        List<SellOutboundRequestDTO> list = new ArrayList<>();
        wmsInReceiptMaterialVos.forEach(t -> {
            SellOutboundRequestDTO requestDTO = new SellOutboundRequestDTO();

            SapParamUtil.validWmsInReceiptMaterial(t);

            WmsInReceiptTaskMaterial one = wmsInReceiptTaskMaterialService.getOne(new LambdaQueryWrapper<WmsInReceiptTaskMaterial>().eq(WmsInReceiptTaskMaterial::getOriginalSystemNumber, t.getOriginalSystemNumber())
                    .eq(WmsInReceiptTaskMaterial::getOriginalSystemLineNumber, t.getOriginalSystemLineNumber()));

            SapParamUtil.validInTaskRecord(one);

            SaleOutboundRequestDTO saleOutboundRequestDTO = JSONObject.parseObject(one.getExtraText(), SaleOutboundRequestDTO.class);
            requestDTO.setDeliveryOrder(t.getOriginalSystemNumber());
            requestDTO.setDeliveryOrderLine(Integer.valueOf(t.getOriginalSystemLineNumber()));
            requestDTO.setInventoryLocation(wmsSapWarehouseMapService.getSapStoreCode(t.getStoreLocation()));
            requestDTO.setBatchCode(t.getBatchCode());
            requestDTO.setNumber(t.getWarehouseQuantity());
            requestDTO.setUnit(saleOutboundRequestDTO.getUnit());
            requestDTO.setUuid(saleOutboundRequestDTO.getUuid());
            list.add(requestDTO);
        });

        List<SellOutboundResponseDTO> sellOutboundResponseDTOS = sapDockingRepeaterService.repeaterSapSellOutbound(list);
        return sellOutboundResponseDTOS;
    }
}
